package model;

import java.io.IOException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel{
	
	/**
	 * default value
	 */
	private static final long serialVersionUID = 1L;
	
	//RESOURCES
	//private SetOfArguments[] sets;
	private String[] heads;
	private ArrayList<SetOfArguments> sets;
	private ArrayList<SetOfArguments> selectedSets;
	private MonteCarlo mc;
	
	/**
	 * CONSTRUCTOR
	 */
	public MyTableModel(){
		super();
		
		//heads
		heads= new String[]{
			"Check", "Call or Put", "Actual price", "Strike price", "Time to maturity",
			"Cost of carry rate", "Discount rate", "Volatility", "Steps", "Nb Simulations" 
		};
		
		//data
		selectedSets = new ArrayList<SetOfArguments>();
		sets = new ArrayList<SetOfArguments>();
		
		//monte carlo computing
		mc= new MonteCarlo();
		
		//load data
		try {
			sets=mc.load();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Return the Class of column
	 * @param columnIndex 
	 * @return Class
	 * 
	 */
	@Override
		public Class< ? > getColumnClass(int columnIndex) {
		return getValueAt(0,columnIndex).getClass();
	}
	
	/**
	 * Indicates if the cell is editable
	 * @param rowIndex 
	 * @param columnIndex 
	 * @return Returns if cell is editable.
	 */
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		
		if (columnIndex == 0) {
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public int getColumnCount() {
		return heads.length;
	}

	@Override
	public int getRowCount() {
		return sets.size();
	}
	
	public String getColumnName(int columnIndex) {
        return heads[columnIndex];
    }
	
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch(columnIndex){
        case 0:
        	return sets.get(rowIndex).isSelected();
        case 1:
            return sets.get(rowIndex).getCallPutFlag();
        case 2:
            return sets.get(rowIndex).getS();
        case 3:
            return sets.get(rowIndex).getX();
        case 4:
            return sets.get(rowIndex).getT();
        case 5:
            return sets.get(rowIndex).getR();
        case 6:
            return sets.get(rowIndex).getB();
        case 7:
            return sets.get(rowIndex).getV();
        case 8:
            return sets.get(rowIndex).getnSteps();
        case 9:
            return sets.get(rowIndex).getnSimulations();
        default:
            return null;
    }
	}

	public void setValueAt(Object value, int rowIndex, int columnIndex) {

		if(columnIndex==0){
			//select row
			if(sets.get(rowIndex).isSelected() == false){
				//change value
				sets.get(rowIndex).setSelected(true);
				//add to selected items list
				selectedSets.add(sets.get(rowIndex));
			}
			//unselect row
			else{
				//change value
				sets.get(rowIndex).setSelected(false);
				//remove from selected items list
				selectedSets.remove(sets.get(rowIndex));
			}
		}
		
		//update table
		fireTableDataChanged();
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	public ArrayList<SetOfArguments> getSelectedSets() {
		return selectedSets;
	}

	public void setSelectedSets(ArrayList<SetOfArguments> selectedSets) {
		this.selectedSets = selectedSets;
	}

	public MonteCarlo getMc() {
		return mc;
	}

	public void setMc(MonteCarlo mc) {
		this.mc = mc;
	}
	
}
